package com.wz.project.test.service;

import com.wz.project.test.domain.Findspace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.ParameterMode;
import javax.persistence.StoredProcedureQuery;

@Service
public class HandTestServiceImpl implements HandTestService {

    @Autowired
    private EntityManager entityManager;

    @Override
    public String allocate(Findspace findspace) {

        String msg = "";
        StoredProcedureQuery storedProcedureQuery = entityManager.createStoredProcedureQuery("proc_find_space");
        StoredProcedureQuery parameter = storedProcedureQuery
                .registerStoredProcedureParameter("v_task_no", String.class, ParameterMode.IN)
                .registerStoredProcedureParameter("v_rkkx1", Integer.class, ParameterMode.IN)
                .registerStoredProcedureParameter("v_rkkx2", Integer.class, ParameterMode.IN)
                .registerStoredProcedureParameter("v_rkkx3", Integer.class, ParameterMode.IN)
                .registerStoredProcedureParameter("v_result", String.class, ParameterMode.OUT);


        parameter.setParameter("v_task_no",findspace.getTaskNo() );
        parameter.setParameter("v_rkkx1",findspace.getRkkx1());
        parameter.setParameter("v_rkkx2",findspace.getRkkx2());
        parameter.setParameter("v_rkkx3",findspace.getRkkx3());





        //执行
        storedProcedureQuery.execute();
        //获取返回结果
        String result = (String) storedProcedureQuery.getOutputParameterValue("v_result");

        if ("9".equals(result)) {
            msg = "处理异常,请重试！";
        }else if("10".equals(result)){
            msg = "分配成功！";
        }else if("3".equals(result)){
            msg="无空闲提升机";
        }else if("2".equals(result)){
            msg="已分配";
        }else if("1".equals(result)){
            msg="无任务号信息";
        }
        return msg;
    }
}
